package com.sky.controller.admin;

import com.github.pagehelper.Page;
import com.sky.dto.DishDTO;
import com.sky.dto.DishPageQueryDTO;
import com.sky.entity.Dish;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.DishService;
import com.sky.vo.DishVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * 菜品管理
 */
@RestController
@RequestMapping("/admin/dish")
@Api(tags = "菜品相关接口")
@Slf4j
public class DishController {

    @Autowired
    private DishService dishService;

    /**
     * 新增菜品
     *
     * @param dishDTO
     * @return
     */
    @PostMapping()
    @ApiOperation("新增菜品")
    public Result<String> add(@RequestBody DishDTO dishDTO) {
        log.info("新增菜品，参数为：{}", dishDTO);
        dishService.add(dishDTO);
        return Result.success();
    }

    /**
     * 菜品管理分页查询
     *
     * @param dishPageQueryDTO 菜品分页查询参数
     * @return result 菜品分页数据
     */
    @GetMapping("/page")
    @ApiOperation("菜品分页查询")
    public Result<PageResult> page(DishPageQueryDTO dishPageQueryDTO) {
        log.info("菜品分页管理，参数为：{}", dishPageQueryDTO);
        PageResult result = dishService.page(dishPageQueryDTO);
        return Result.success(result);
    }

    /**
     * 根据id查询菜品
     *
     * @param id
     */
    @GetMapping("/{id}")
    @ApiOperation("根据id查询菜品")
    public Result<DishVO> getById(@PathVariable Long id) {
        log.info("根据id查询菜品，参数为:{}", id);
        DishVO dishVO = dishService.getById(id);
        return Result.success(dishVO);

    }

    /**
     * 根据分类id查询菜品
     *
     * @param categoryId
     */
    @GetMapping("/list")
    @ApiOperation("根据分类id查询菜品")
    public Result<List<Dish>> getDishByCategoryId(Long categoryId) {
        log.info("根据分类id查询菜品，参数为：{}", categoryId);
        List<Dish> list = dishService.getDishByCategoryId(categoryId);
        return Result.success(list);
    }



    /**
     * 批量删除菜品
     *
     * @param ids
     * @return
     */
    @DeleteMapping()
    @ApiOperation("批量删除菜品")
    public Result delete(@RequestParam String ids) {
        log.info("批量删除菜品，参数为：{}", ids);
        List<Long> idList = new ArrayList<>();
        for (String s : ids.replace("，", ",").split(",")) {
            String trim = s.trim();
            if (!trim.isEmpty()) {
                Long l = Long.valueOf(trim);
                idList.add(l);
            }
        }
        dishService.deleteDish(idList);
        return Result.success();
    }

    /**
     * 修改菜品
     *
     * @param dishDTO
     * @return
     */
    @PutMapping()
    @ApiOperation("修改菜品")
    public Result update(@RequestBody DishDTO dishDTO) {
        log.info("修改菜品，参数为：{}", dishDTO);
        dishService.update(dishDTO);
        return Result.success();
    }

    /**
     * 菜品启售/停售
     *
     * @param status
     * @param id
     * @return
     */
    @PostMapping("/status/{status}")
    @ApiOperation("菜品启售/停售")
    public Result<String> startOrStop(@PathVariable Integer status, Long id) {
        log.info("菜品启售/停售，参数为：{},{}", status, id);
        dishService.startOrStop(status, id);
        return Result.success();
    }
}
